<template>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px">

    <el-form-item label="用户名" prop="username">
      <el-input
        type="text"
        placeholder="请填写用户名"
        v-model="ruleForm.username"
        autocomplete="off">
      </el-input>
    </el-form-item>

    <el-form-item label="用户权限" prop="authority">
      <el-input
        type="text"
        v-model="ruleForm.authority"
        :disabled="true">
      </el-input>
    </el-form-item>

    <el-form-item label="登录密码" prop="password">
      <el-input
        type="password"
        placeholder="请填写登录密码"
        v-model="ruleForm.password"
        autocomplete="off">
      </el-input>
    </el-form-item>

    <el-form-item label="确认密码" prop="confirmingPassword">
      <el-input
        type="password"
        placeholder="请确认密码"
        v-model="ruleForm.confirmingPassword"
        autocomplete="off">
      </el-input>
    </el-form-item>

    <el-form-item label="性别" prop="gender">
      <el-radio v-model="ruleForm.gender" label="男">男</el-radio>
      <el-radio v-model="ruleForm.gender" label="女">女</el-radio>
    </el-form-item>

    <el-form-item label="出生日期" prop="birthday">
      <el-date-picker
        v-model="ruleForm.birthday"
        type="date"
        placeholder="请选择出生日期">
      </el-date-picker>
    </el-form-item>

    <el-form-item
      prop="email"
      label="邮箱"
    >
      <el-input
        placeholder="请填写正确的邮箱地址"
        v-model="ruleForm.email">
      </el-input>
    </el-form-item>

    <el-form-item label="手机号码" prop="phone">
      <el-input
        type="text"
        placeholder="请填写11位手机号"
        v-model="ruleForm.phone"
        autocomplete="off">
      </el-input>
    </el-form-item>

    <el-form-item label="发货地址" prop="address">
      <el-input
        type="text"
        placeholder="请填写发货地址"
        v-model="ruleForm.address"
        autocomplete="off">
      </el-input>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
      <el-button type="info" @click="resetForm('ruleForm')">重置</el-button>
      <el-button type="warning" @click="$router.push('/')">返回首页</el-button>
    </el-form-item>

  </el-form>
</template>

<script>
export default {
  name: "NormalRegister",
  data() {
    let validateConfirmingPassword = (rule, value, callback) => {
      if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: '',
        authority: '供应商',
        password: '',
        confirmingPassword: '',
        gender: '',
        birthday: '',
        email: '',
        phone: '',
        address: '',
        status: 1
      },
      rules: {
        username: [
          {required: true, message: '请填写用户名！', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请填写登录密码！', trigger: 'blur'},
        ],
        confirmingPassword: [
          {required: true, message: '请再次输入密码！', trigger: 'blur'},
          {validator: validateConfirmingPassword, trigger: 'blur'}
        ],
        gender: [
          {required: true, message: '请选择性别！', trigger: 'blur'},
        ],
        birthday: [
          {required: true, message: '请选择出生日期！', trigger: 'blur'},
        ],
        email: [
          {required: true, message: '请输入邮箱地址！', trigger: 'blur'},
          {type: 'email', message: '请输入正确的邮箱地址！', trigger: ['blur']}
        ],
        phone: [
          {required: true, message: '请输入手机号!', trigger: 'blur'},
          {min: 11, max: 11, message: '手机号必须为11位！', trigger: ['blur']}
        ],
        address: [
          {required: true, message: '请输入收货地址！', trigger: 'blur'},
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      const _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const registerUser = {
            username: this.ruleForm.username,
            authority: this.ruleForm.authority,
            password: this.ruleForm.password,
            gender: this.ruleForm.gender,
            birthday: this.ruleForm.birthday,
            email: this.ruleForm.email,
            phone: this.ruleForm.phone,
            address: this.ruleForm.address,
            status: this.ruleForm.status
          }
          _this.$axios.post('/register', registerUser).then((res) => {

            _this.$message({
              message: '恭喜！注册成功！',
              type: 'success'
            });

            _this.$router.push("/login")
          })


        } else {
          _this.$message.error('注册失败！请确认表单信息填写是否正确！');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

.text {
  font-size: 14px;
}

.item {
  padding: 18px 0;
}

.box-card {
  width: 480px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.card-title {
  display: flex;
  justify-content: center;
  font-weight: bold;
  font-size: 18px;
}

.el-button {
  margin-right: 20px;
}
</style>